package com.songcha.sakura.mvvm.comic.episode

import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.sakura.bean.ComicM3u8Bean
import com.songcha.sakura.config.BaseConfig
import io.reactivex.Observable
import org.jsoup.Jsoup

class ComicM3u8Repository : BaseRepository()  {

     fun getEpisodeM3u8(episode:String): Observable<ComicM3u8Bean> {
         return createNetworkObservable {
             //http://www.dmh8.com/list/2_$page.html
             val document = Jsoup.connect("${BaseConfig.BASE_URL}$episode").get()
             var title=document.getElementsByClass("tips close-box")[0].getElementsByTag("li")[0].text()
             if(title.length>5){
                 title=title.substring(5,title.length)
             }


             val str=document.toString()

             var m3u8Url=""
             val start=str.indexOf("},\"url\":")
             val end=str.indexOf(",\"url_next\":")
             if(start>-1 && end>-1){
                 m3u8Url=str.substring(start+9,end-1)
                 m3u8Url=m3u8Url.replace("\\/","/")
             }

             val bean=ComicM3u8Bean(m3u8Url,title)
             it.onNext(bean)
             it.onComplete()
         }

        //return RetrofitClient.api.getHomeCartList().await()
    }



}